using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Business.Entities.Config;

namespace OR.Lazarus.Business.Entities.Config.Lazarus
{
	/// <summary>
	/// The root node of a config document.
	/// </summary>
   public class ConfigDetail : EntityBase
   {
		protected System.String mName;
		protected System.String mTitle;
		protected string mFolder;
		protected string mCompiledOutput;
		protected string mManifestOutput;
		protected string mDescription;
	
		protected Derived.ClassReferenceCollection mEngineTemplates;
		protected Derived.TierCollection mTiers;

		public ConfigDetail()
		{
			mEngineTemplates = new Derived.ClassReferenceCollection();
			mEngineTemplates.ParentEntity = this;
			mTiers = new Derived.TierCollection();
			mTiers.ParentEntity = this;
	
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public System.String Title
		{
			get
			{
				return ( mTitle );
			}
			set
			{
				mTitle = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Folder
		{
			get
			{
				return ( mFolder );
			}
			set
			{
				mFolder = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string CompiledOutput
		{
			get
			{
				return ( mCompiledOutput );
			}
			set
			{
				mCompiledOutput = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string ManifestOutput
		{
			get
			{
				return ( mManifestOutput );
			}
			set
			{
				mManifestOutput = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.ClassReferenceCollection EngineTemplates
		{
			get
			{
				return ( mEngineTemplates );
			}
			set
			{
				mEngineTemplates = value;
			}
		}

		public Derived.TierCollection Tiers
		{
			get
			{
				return ( mTiers );
			}
			set
			{
				mTiers = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mTitle = DataUtility.GetString( pSource, "title" );
			mFolder = DataUtility.GetString( pSource, "folder" );
			mCompiledOutput = DataUtility.GetString( pSource, "compiledoutput" );
			mManifestOutput = DataUtility.GetString( pSource, "manifestoutput" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lcfg:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vEngineTemplates = ( XmlElement )pSource.SelectSingleNode( "lcfg:engine-templates", PersistenceConfig.Namespaces );
			
			if ( vEngineTemplates != null )
			{
				mEngineTemplates.LoadXml( vEngineTemplates );
			}
			XmlElement vTiers = ( XmlElement )pSource.SelectSingleNode( "lcfg:tiers", PersistenceConfig.Namespaces );
			
			if ( vTiers != null )
			{
				mTiers.LoadXml( vTiers );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lcfg", "config", "http://schemas.icitadel.com/Lazarus/Config.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mTitle ) )
			{
				vNewElement.SetAttribute( "title", mTitle );
			}
					
			if ( !String.IsNullOrEmpty( mFolder ) )
			{
				vNewElement.SetAttribute( "folder", mFolder );
			}
					
			if ( !String.IsNullOrEmpty( mCompiledOutput ) )
			{
				vNewElement.SetAttribute( "compiledoutput", mCompiledOutput );
			}
					
			if ( !String.IsNullOrEmpty( mManifestOutput ) )
			{
				vNewElement.SetAttribute( "manifestoutput", mManifestOutput );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lcfg", "description", "http://schemas.icitadel.com/Lazarus/Config.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mEngineTemplates.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mTiers.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.ConfigDetail v_oReturn = new Derived.ConfigDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Title = mTitle;
			v_oReturn.Folder = mFolder;
			v_oReturn.CompiledOutput = mCompiledOutput;
			v_oReturn.ManifestOutput = mManifestOutput;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.ClassReferenceDetail vDetail in mEngineTemplates )
			{
				v_oReturn.EngineTemplates.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.TierDetail vDetail in mTiers )
			{
				v_oReturn.Tiers.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.ConfigDetail CloneTyped()
		{
			return ( ( Derived.ConfigDetail )this.Clone() );
		}
   }
}
   